package weaver.customAction;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.sql.ResultSet;

/**
 * Created by WANGJIE-PC on 2017/5/10.
 */
public class CustomNumProcessAction extends BaseBean implements Action{

    @Override
    public String execute(RequestInfo requestInfo) {
        writeLog("===============CustomNumProcessAction开始执行================");
        //获取当前流程requestid
        String requestid = Util.null2String(requestInfo.getRequestid());
        //获取表单名称
        String mainTableName = requestInfo.getRequestManager().getBillTableName();
        if (requestid == "null" || mainTableName == "null"){
            writeLog("查询不到requestid或者是表单名称");
            return SUCCESS;
        }
        //查询该流程的自定义流水号
        RecordSet rs = new RecordSet();
        String sql = "select * from "+mainTableName+" where requestid = '"+requestid+"'";
        rs.execute(sql);

        String bh = "";
        if (rs.next()){
            bh  = rs.getString("xmbh");//不可使用null2String
            writeLog("查询到的编号为："+bh);
            //更新编号的流水号
            if (bh == null){
                writeLog("CustomNumProcessAction:查询的编号为null");
                return SUCCESS;
            }
            String newbh = mark(bh);
            //判断新编号
            if(newbh == null || newbh.trim().length() == 0){
                writeLog("CustomNumProcessAction:流程编号为:"+requestid+"的流程编号修改失败了！");
                return SUCCESS;
            }
            sql = "UPDATE  "+mainTableName+" SET xmbh = '" + newbh +"' WHERE requestid = " + requestid;
            rs.execute(sql);
            writeLog("CustomNumProcessAction:更新流程编号成功===========================");
        }else {
            writeLog("requestid："+requestid+"查询不到这条数据！");
        }

        return SUCCESS;

    }

    /**
     * 处理编号
     * @return
     */
    public String mark(String oldbh){
        //除流水号前缀
        String qianzhui = oldbh.substring(0, oldbh.lastIndexOf("〕")+1);
        //新流水号
        String newlsh = String.valueOf(new Integer(oldbh.substring(oldbh.lastIndexOf("〕")+1,oldbh.length()-1)));
        //拼接返回
        return qianzhui+newlsh;

    }

}
